package com.iwhalecloud.bss.kite.cucc.service.attr;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteSpringUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.IIndustryService;
import com.iwhalecloud.bss.kite.cucc.client.api.attr.IUnicomRegionService;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.KiteIndustryConfig;
import com.iwhalecloud.bss.kite.cucc.client.dto.attr.UnicomRegion;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;

/**
 * 行业属性处理
 * Created by huanghaosheng on 2020/11/12 21:47
 */
public class LocalIndustryAttrHelper {

    /**
     * 初始化可选行业
     *
     * @param attr
     */
    public static void initIndustry(KiteAttr attr) {
        IIndustryService industryService = KiteSpringUtils.getBeanHighPriority(IIndustryService.class);
        List<KiteIndustryConfig> kiteIndustryList = industryService.listSubIndustry("");
        if (KiteListUtils.isEmpty(kiteIndustryList)) {
            return;
        }
        List<KiteAttrValue> kiteAttrValues = kiteIndustryList.stream().map(kiteIndustry -> {
            KiteAttrValue kiteAttrValue = new KiteAttrValue();
            kiteAttrValue.setAttrValue(kiteIndustry.getIndustryCode());
            kiteAttrValue.setAttrValueName(kiteIndustry.getIndustryName());
            return kiteAttrValue;
        }).collect(Collectors.toList());
        attr.setAttrValues(kiteAttrValues);
    }

    /**
     * 加载产品线
     *
     * @param parentIndustryCode
     */
    public static List<KiteAttrValue> loadProductLine(String parentIndustryCode) {
        List<KiteAttrValue> kiteAttrValues = new ArrayList<>();
        if (KiteStringUtils.isNotEmpty(parentIndustryCode)) {
            IIndustryService industryService = KiteSpringUtils.getBeanHighPriority(IIndustryService.class);
            List<KiteIndustryConfig> kiteIndustryList = industryService.listSubIndustry(parentIndustryCode);
            if (KiteListUtils.isEmpty(kiteIndustryList)) {
                return new ArrayList<>();
            }
            kiteAttrValues = kiteIndustryList.stream().map(kiteIndustry -> {
                KiteAttrValue kiteAttrValue = new KiteAttrValue();
                kiteAttrValue.setAttrValue(kiteIndustry.getIndustryCode());
                kiteAttrValue.setAttrValueName(kiteIndustry.getIndustryName());
                kiteAttrValue.setAttrValueDesc(kiteIndustry.getIndustryName());
                return kiteAttrValue;
            }).collect(Collectors.toList());
        }
        return kiteAttrValues;
    }

    /**
     * 设置某个属性必填
     *
     * @param inst
     * @param fieldName
     */
    public static void setNonnull(KiteInst inst, String fieldName) {
        KiteAttr kiteAttr = inst.getAttr(fieldName);
        if (Objects.nonNull(kiteAttr)) {
            kiteAttr.setIsNull(KeyConsts.IFTRUE_F);
        }
    }

    /**
     * 设置某个属性非必填
     *
     * @param inst
     * @param fieldName
     */
    public static void setNullable(KiteInst inst, String fieldName) {
        KiteAttr kiteAttr = inst.getAttr(fieldName);
        if (Objects.nonNull(kiteAttr)) {
            kiteAttr.setIsNull(KeyConsts.IFTRUE_T);
        }
    }

    /**
     * 设置某个属性可编辑
     *
     * @param inst
     * @param fieldName
     */
    public static void setEditable(KiteInst inst, String fieldName) {
        KiteAttr kiteAttr = inst.getAttr(fieldName);
        if (Objects.nonNull(kiteAttr)) {
            kiteAttr.setIsEdit(KeyConsts.IFTRUE_T);
        }
    }

    /**
     * 设置多个属性可编辑
     *
     * @param inst
     * @param fieldNames
     */
    public static void setMultipleEditable(KiteInst inst, String... fieldNames) {
        Arrays.asList(fieldNames).stream().forEach(fieldName -> {
            KiteAttr kiteAttr = inst.getAttr(fieldName);
            if (Objects.nonNull(kiteAttr)) {
                kiteAttr.setIsEdit(KeyConsts.IFTRUE_T);
            }
        });
    }

    /**
     * 设置某个属性不可编辑
     *
     * @param inst
     * @param fieldName
     */
    public static void setNotEditable(KiteInst inst, String fieldName) {
        KiteAttr kiteAttr = inst.getAttr(fieldName);
        if (Objects.nonNull(kiteAttr)) {
            kiteAttr.setIsEdit(KeyConsts.IFTRUE_F);
        }
    }

    /**
     * 设置多个属性不可编辑
     *
     * @param inst
     * @param fieldNames
     */
    public static void setMultipleNotEditable(KiteInst inst, String... fieldNames) {
        Arrays.asList(fieldNames).stream().forEach(fieldName -> {
            KiteAttr kiteAttr = inst.getAttr(fieldName);
            if (Objects.nonNull(kiteAttr)) {
                kiteAttr.setIsEdit(KeyConsts.IFTRUE_F);
            }
        });
    }

    public static String getRegionName(String regionCode) {
        IUnicomRegionService unicomRegionService = KiteSpringUtils.getBeanHighPriority(IUnicomRegionService.class);
        UnicomRegion unicomRegions = unicomRegionService.getRegion(regionCode);
        String regionName = "";
        if (!KiteObjectUtils.isEmpty(unicomRegions)) {
            regionName = unicomRegions.getRegionName();
        }
        return regionName;
    }

    public static UnicomRegion getRegion(String regionCode) {
        IUnicomRegionService unicomRegionService = KiteSpringUtils.getBeanHighPriority(IUnicomRegionService.class);
        UnicomRegion unicomRegions = unicomRegionService.getRegion(regionCode);
        if (!KiteObjectUtils.isEmpty(unicomRegions)) {
            return unicomRegions;
        }
        return new UnicomRegion();
    }

    public static void transformAddrCodeToName(String addrFileNames, KiteAttr kiteAttr) {
        if (KiteStringUtils.contains(addrFileNames, kiteAttr.getFieldName())) {
            UnicomRegion unicomRegion = getRegion(kiteAttr.getValue());
            String regionName = unicomRegion.getRegionName();
            if (KiteStringUtils.isNotEmpty(regionName)) {
                kiteAttr.setValueDesc(regionName);
                if (KiteListUtils.isEmpty(kiteAttr.getAttrValues())) {
                    KiteAttrValue kiteAttrValue = new KiteAttrValue();
                    kiteAttrValue.setAttrValue(unicomRegion.getRegionCode());
                    kiteAttrValue.setAttrValueName(unicomRegion.getRegionName());
                    kiteAttr.setAttrValues(Arrays.asList(kiteAttrValue));
                    ;
                }
            }
        }
    }
}
